﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApp.BLL.Base;
using WebApp.Common;
using WebApp.DAL;
using WebApp.Models;


namespace WebApp.BLL.Manage
{
    public class S_user_tokenService : BaseService<S_user_token>
    {
        #region 构造

        public S_user_tokenService() : base(new S_user_tokenRepository()) { }

        #endregion


        #region SelectByUserId

        public S_user_token SelectByUserId(int user_id)
        {
            return this.CurrentRepository.Entities.FirstOrDefault(p => p.User_id == user_id);
        }

        #endregion

        #region SelectByAccessToken

        public S_user_token SelectByAccessToken(string access_token)
        {
            return this.CurrentRepository.Entities.FirstOrDefault(p => p.Access_token == access_token);
        }

        #endregion

        #region AddToken

        public void AddToken(int user_id, string access_token, DateTime expireTime)
        {
            var model = this.SelectByUserId(user_id);
            if (model == null)
            {
                model = new S_user_token();
                model.User_id = user_id;
                model.Access_token = access_token;
                model.Expiretime = expireTime;
                model.Devicetype = "";
                model.OStype = "";
                model.Ipaddress = Utils.GetClientIP();

                this.CurrentRepository.Add(model);
            }
            else
            {
                model.Access_token = access_token;
                model.Expiretime = expireTime;
                model.Devicetype = "";
                model.OStype = "";
                model.Ipaddress = Utils.GetClientIP();

                this.CurrentRepository.Update(model);
            }
        }

        #endregion

    }
}
